/**
 * ReportGear(2011)
 */
package com.reportgear.designer.gui.component;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.border.AbstractBorder;

import net.abbp.swing.util.GuiUtils;

/**
 * 线边框
 * 
 * @version 1.0 2011-4-27
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ModLineBorder extends AbstractBorder {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -4182682156721594804L;
	public static final int TOP = 1;
	public static final int LEFT = 2;
	public static final int BOTTOM = 4;
	public static final int RIGHT = 8;
	private int modifiers;
	private int thickness;
	private Color lineColor;

	public ModLineBorder(int lineColor) {
		this(lineColor, GuiUtils.getTitleLineBorderColor(), 1);
	}

	public ModLineBorder(int modifier, Color lineColor) {
		this(modifier, lineColor, 1);
	}

	public ModLineBorder(int modifiers, Color lineColor, int thickness) {
		this.modifiers = modifiers;
		this.lineColor = lineColor;
		this.thickness = thickness;
	}

	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
		Color color = g.getColor();
		g.setColor(this.lineColor);

		for (int i = 0; i < this.thickness; ++i) {
			if ((this.modifiers & 0x1) != 0) {
				g.drawLine(x + i, y + i, x + width - i - 1, y + i);
			}
			if ((this.modifiers & 0x2) != 0) {
				g.drawLine(x + i, y + i, x + i, y + height - i - 1);
			}
			if ((this.modifiers & 0x4) != 0) {
				g.drawLine(x + i, y + height - i - 1, x + width - i - 1, y + height - i - 1);
			}
			if ((this.modifiers & 0x8) == 0) {
				continue;
			}

			g.drawLine(x + width - i - 1, y + i, x + width - i - 1, height - i - i - 1);
		}

		g.setColor(color);
	}

	public Insets getBorderInsets(Component c) {
		return getBorderInsets(c, new Insets(this.thickness, this.thickness, this.thickness, this.thickness));
	}

	public Insets getBorderInsets(Component c, Insets insets) {
		insets.left = (insets.top = insets.right = insets.bottom = this.thickness);
		if (((this.modifiers & 0x1) != 0) && ((this.modifiers & 0x4) == 0)) {
			insets.bottom = 0;
		}
		if (((this.modifiers & 0x2) != 0) && ((this.modifiers & 0x8) == 0)) {
			insets.right = 0;
		}
		if (((this.modifiers & 0x4) != 0) && ((this.modifiers & 0x1) == 0)) {
			insets.top = 0;
		}
		if (((this.modifiers & 0x8) != 0) && ((this.modifiers & 0x2) == 0)) {
			insets.left = 0;
		}

		return insets;

	}

	public int getModifiers() {
		return this.modifiers;
	}

	public void setModifiers(int midifiers) {
		this.modifiers = midifiers;
	}

	public Color getLineColor() {
		return this.lineColor;
	}

	public int getThickness() {
		return this.thickness;
	}
}